package com.lb.jwitter.action;

import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblQLDTTXADMKetQuaXetTuyen;
import com.lb.jwitter.service.TblQLDTTXADMKetQuaXetTuyenService;
import com.lb.jwitter.service.TblQLDTTXADMThangDiemService;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemTong;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemTongService;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemXetTuyenService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoService;
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyenService;

import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayTo;

import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayToService;

import com.lb.jwitter.service.TblQLDTTXADMGiayToService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblQLDTTXAHoSoDiemXetTuyenInsertAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long id;

	public void setId(Long id) {
		this.id = id;
	}

	private int hoSoId;

	public void setHoSoId(int hoSoId) {
		this.hoSoId = hoSoId;
	}

	public int getHoSoId() {
		return hoSoId;
	}

	/*private String diemTong;

	public String getDiemTong() {
		return diemTong;
	}

	public void setDiemTong(String diemTong) {
		this.diemTong = diemTong;
	}

	private String maXetTuyen;

	public String getMaXetTuyen() {
		return maXetTuyen;
	}

	public void setMaXetTuyen(String maXetTuyen) {
		this.maXetTuyen = maXetTuyen;
	}*/

	private TblQLDTTXAHoSoXetTuyen tblQLDTTXAHoSoXetTuyen;

	public TblQLDTTXAHoSoXetTuyen getTblQLDTTXAHoSoXetTuyen() {
		return tblQLDTTXAHoSoXetTuyen;
	}

	private TblQLDTTXAHoSoDiemXetTuyenService tblQLDTTXAHoSoDiemXetTuyenService;

	private TblQLDTTXAHoSoXetTuyenService tblQLDTTXAHoSoXetTuyenService;
	/*private TblQLDTTXAHoSoDiemTongService tblQLDTTXAHoSoDiemTongService;
	private Collection<TblQLDTTXADMKetQuaXetTuyen> tblQLDTTXADMKetQuaXetTuyens;
	private TblQLDTTXADMKetQuaXetTuyenService tblQLDTTXADMKetQuaXetTuyenService;*/
	private String tblQLDTTXAHoSoDiemXetTuyenStr;

	public void setTblQLDTTXAHoSoDiemXetTuyenStr(
			String tblQLDTTXAHoSoDiemXetTuyenStr) {
		this.tblQLDTTXAHoSoDiemXetTuyenStr = tblQLDTTXAHoSoDiemXetTuyenStr;
	}

	public String getTblQLDTTXAHoSoDiemXetTuyenStr() {
		return tblQLDTTXAHoSoDiemXetTuyenStr;
	}

	private TblQLDTTXADMThangDiemService tblQLDTTXADMThangDiemService;

	private int preInsert(Message msg, TblQLDTTXABienNhanHoSo obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postInsert(Message msg, TblQLDTTXABienNhanHoSo obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HoSo_DiemXetTuyen",
					"them")
					|| !tblQuyenChiTietService.checkRight(taiKhoanDangNhap
							.getMaNhomQuyen().getId(),
							"tbl_QLDTTXA_HoSo_DiemTong", "them")) {
				return "noprivilege";
			}

			tblQLDTTXAHoSoDiemXetTuyenService = (TblQLDTTXAHoSoDiemXetTuyenService) context
					.getBean("tbl_QLDTTXA_HoSo_DiemXetTuyenClient");

			tblQLDTTXAHoSoXetTuyenService = (TblQLDTTXAHoSoXetTuyenService) context
					.getBean("tbl_QLDTTXA_HoSoXetTuyenClient");

			tblQLDTTXADMThangDiemService = (TblQLDTTXADMThangDiemService) context
					.getBean("tbl_QLDTTXA_DM_ThangDiemClient");

			/*tblQLDTTXAHoSoDiemTongService = (TblQLDTTXAHoSoDiemTongService) context
					.getBean("tbl_QLDTTXA_HoSo_DiemTongClient");
			tblQLDTTXADMKetQuaXetTuyenService =(TblQLDTTXADMKetQuaXetTuyenService) context.getBean("tbl_QLDTTXA_DM_KetQuaXetTuyenClient");*/
			msg = new Message();
			tblQLDTTXAHoSoXetTuyen = tblQLDTTXAHoSoXetTuyenService
					.gettblQLDTTXAHoSoXetTuyen(new Long(hoSoId));

			JSONArray jsonTblQLDTTXAHoSoDiemXetTuyen = (JSONArray) JSONSerializer
					.toJSON(tblQLDTTXAHoSoDiemXetTuyenStr);
			for (int i = 0; i < jsonTblQLDTTXAHoSoDiemXetTuyen.size(); i++) {
				JSONObject jsObj = jsonTblQLDTTXAHoSoDiemXetTuyen
						.getJSONObject(i);
				TblQLDTTXAHoSoDiemXetTuyen obj0 = new TblQLDTTXAHoSoDiemXetTuyen();

				obj0.setThangDiemId(tblQLDTTXADMThangDiemService
						.gettblQLDTTXADMThangDiem(new Long(jsObj
								.getInt("thangDiemId"))));
				obj0.setHoSoId(tblQLDTTXAHoSoXetTuyen);
				tblQLDTTXAHoSoDiemXetTuyenService
						.createtblQLDTTXAHoSoDiemXetTuyen(obj0);
			}
			//Double diemTong = obj.getDouble("diemTong");

			/*TblQLDTTXAHoSoDiemTong tblQLDTTXAHoSoDiemTong = new TblQLDTTXAHoSoDiemTong();
			// set HosoId
			tblQLDTTXAHoSoDiemTong.setHoSoId(tblQLDTTXAHoSoXetTuyen);
			// Set canBoId
			tblQLDTTXAHoSoDiemTong.setCanBoId(taiKhoanDangNhap.getCanBoId());
			// set diemTong
			Double diemT = Double.parseDouble(diemTong);
			
			tblQLDTTXAHoSoDiemTong.setDiemTong(diemT);
			// set ngay xet tuyen

			GregorianCalendar c = new GregorianCalendar();
			c.setTime(new Date());
			XMLGregorianCalendar d = DatatypeFactory.newInstance()
					.newXMLGregorianCalendar(c);
			tblQLDTTXAHoSoDiemTong.setNgayXetTuyen(d);

			// set ket qua
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			if (true) {
				ConditionReport con = new ConditionReport();
				con.setCol("ma");
				con.setValue1(maXetTuyen);
				con.setOperator("eq");
				cons.add(con);
			}
			tblQLDTTXADMKetQuaXetTuyens = tblQLDTTXADMKetQuaXetTuyenService
					.findtblQLDTTXADMKetQuaXetTuyenByCondition(cons, 1, 1);
			for (TblQLDTTXADMKetQuaXetTuyen tblQLDTTXADMKetQuaXetTuyen : tblQLDTTXADMKetQuaXetTuyens)
				tblQLDTTXAHoSoDiemTong.setKetQuaId(tblQLDTTXADMKetQuaXetTuyen);

			tblQLDTTXAHoSoDiemTongService
					.createtblQLDTTXAHoSoDiemTong(tblQLDTTXAHoSoDiemTong);

			// set trang thai la da xet tuyen
			if (maXetTuyen.equalsIgnoreCase("KD")) {
				tblQLDTTXAHoSoXetTuyen.setTrangThaiId(20);
			} else {
				tblQLDTTXAHoSoXetTuyen.setTrangThaiId(21);
			}

			tblQLDTTXAHoSoXetTuyenService
					.updatetblQLDTTXAHoSoXetTuyen(tblQLDTTXAHoSoXetTuyen);*/
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Insert tbl_QLDTTXA_BienNhanHoSo Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	public Long getId() {
		return id;
	}
}
